package com.threeonemeeting.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.threeonemeeting.R;
import com.threeonemeeting.list.TradesInfoList;
import com.threeonemeeting.listcache.MeetingTradesInfoCache;
import com.threeonemeeting.model.MeetingTradesInfo;

public class MeetingTradesInfoListAdapter extends
		ArrayAdapter<MeetingTradesInfo> {

	private TradesInfoList mTradesItems = null;
	
	public MeetingTradesInfoListAdapter(Activity activity) {
		super(activity, 0);
	}

	public void setTradesList(TradesInfoList tradesList) {
		mTradesItems = tradesList;
		notifyDataSetChanged();
	}

	public TradesInfoList getTradesList() {
		return mTradesItems;
	}

	@Override
	public void add(MeetingTradesInfo tradeInfo) {
		if (mTradesItems == null)
			mTradesItems = new TradesInfoList();

		mTradesItems.addMeetingTradesInfo(tradeInfo);
	}

	public void clear() {
		if (mTradesItems != null) {
			mTradesItems.listMeetingTradesInfo.clear();
		}
	}

	@Override
	public int getCount() {
		if (mTradesItems == null)
			return 0;

		return mTradesItems.size();
	}

	@Override
	public MeetingTradesInfo getItem(int position) {
		return mTradesItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {

		Activity activity = (Activity) getContext();

		MeetingTradesInfo categoryInfo = getItem(position);

		View rowView = convertView;
		MeetingTradesInfoCache viewCache;
		if (rowView == null) {
			LayoutInflater inflater = activity.getLayoutInflater();
			rowView = inflater.inflate(R.layout.lvi_trade, null);
			viewCache = new MeetingTradesInfoCache(rowView,
					categoryInfo.nCategoryId, categoryInfo.strCategoryName);
			rowView.setTag(viewCache);
		} else {
			viewCache = (MeetingTradesInfoCache) rowView.getTag();
		}

		// 会议标题
		TextView meetingCategoryNameView = viewCache
				.getMeetingCategoryNameTextView();

		String meetingCategoryName = categoryInfo.strCategoryName;
		if (meetingCategoryName != null && meetingCategoryName.length() > 12) {
			meetingCategoryName = meetingCategoryName.substring(0, 12);
			meetingCategoryName += "...";
		}
		meetingCategoryNameView.setText(meetingCategoryName);

		return rowView;
	}
}
